package com.tony.business.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;


/**
 * @author x
 */
@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    ApplicationContext applicationContext;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    protected void doRegister(RedisMessageListenerContainer listenerContainer) {
        //监听指定的redis数据库
        RedisStandaloneConfiguration configuration = (RedisStandaloneConfiguration) applicationContext.getBean("redis");
        listenerContainer.addMessageListener(this, new PatternTopic("__keyevent@" + configuration.getDatabase() + "__:expired"));
    }

    @Override
    public void onMessage(Message message, @Nullable byte[] pattern) {

    }

}
